
#include "main.h"

#define SOFTWAREVERSION "2.0.2"
/**
 * @brief help
 *
 */
static void help(void)
{
	printf("monitorctl { -h | -c | -m | -v | -i | -a } type\n\
-h		--help\n\
-v		--show-version\n\
-c		--show-cpu-usage\n\
-m		--show-memory-usage\n\
-i		--show-storage-usage\n\
-a		--show-all\n\
type	[host | container]\n");
}

/**
 * @brief Get the Option Param object
 *
 * @param argc
 * @param argv
 * @return uint8_t
 */
uint8_t getOptionParam(int argc, char **argv)
{
	int ch;
	uint8_t cpu_usage = 0;
	uint8_t mem_usage = 0;
	uint8_t docker_num = 0;
	uint8_t i;
	char storage_size[1024] = {0};

	ch = getopt(argc, argv, "hvc:m:i:a:");
	if (ch == -1)
	{
	    printf("Command parameter error!\n");
		help();
		return FALSE;
	}

	dockerConfig = (dockerType *)malloc(sizeof(dockerType) * 64);

	switch (ch)
	{
	case 'h':
		help();
		break;

	case 'c':
		if (!strcmp(optarg, "host"))
		{
			cpu_usage = get_cpuUsage();
			printf("name: host\nCPU usage: %d%%\n", cpu_usage);
		}

		else if (!strcmp(optarg, "container"))
		{
			docker_num = cpu_scanDocker();
			printf("container number :%d\n", docker_num);
			for (i = 0; i < docker_num; i++)
			{
				printf("name: %s\nCPU usage: %s\n", dockerConfig[i].name, dockerConfig[i].cpu_usage);
			}
		}

		else
		{
			help();
		}
		break;

	case 'm':
		if (!strcmp(optarg, "host"))
		{
			mem_usage = get_memUsage();
			printf("name: host\nmemory usage: %d%%\n", mem_usage);
		}

		else if (!strcmp(optarg, "container"))
		{
			docker_num = cpu_scanDocker();
			printf("container number :%d\n", docker_num);
			for (i = 0; i < docker_num; i++)
			{
				printf("name: %s\nmemory usage: %s\n", dockerConfig[i].name, dockerConfig[i].mem_usage);
			}
		}

		else
		{
			help();
		}
		break;

	case 'i':
		if (!get_flashUsage(storage_size))
		{
		}

		if (!strcmp(optarg, "host"))
		{
			printf("name: host\nstorage usage: %s\n", storage_size);
		}

		else if (!strcmp(optarg, "container"))
		{
			docker_num = cpu_scanDocker();
			printf("container number :%d\n", docker_num);
			for (i = 0; i < docker_num; i++)
			{
				printf("name: %s\nstorage usage: %s\n", dockerConfig[i].name, storage_size);
			}
		}

		else
		{
			help();
		}
		break;

	case 'a':
		/**get the cpu usage*/
		cpu_usage = get_cpuUsage();
		/**get the memory usage*/
		mem_usage = get_memUsage();
		/**get the flash*/
		if (!get_flashUsage(storage_size))
		{
		}

		if (!strcmp(optarg, "host"))
		{
			printf("name: host\nCPU usage: %d%%\nmemory usage: %d%%\nstorage usage: %s\n",
				    cpu_usage, mem_usage, storage_size);
		}

		else if (!strcmp(optarg, "container"))
		{
			docker_num = cpu_scanDocker();
			printf("container number :%d\n\n", docker_num);
			for (i = 0; i < docker_num; i++)
			{
				printf("name: %s\nCPU usage: %s\nmemory usage: %s\nstorage usage: %s\n\n",
					    dockerConfig[i].name, dockerConfig[i].cpu_usage, dockerConfig[i].mem_usage, storage_size);
			}
		}

		else
		{
			help();
		}
		break;

	case 'v':
		printf("version: V%s\n", SOFTWAREVERSION);
		break;

	default:
		help();
		break;
	}

	free(dockerConfig);
	dockerConfig = NULL;

	return TRUE;
}
